package com.onepiece.shipelves.common.utils;

/**
 *
 * @author guoyanjiang
 * @date 2018/4/13
 */
public class VersionUtil {

    /**
     * 比较版本号大小
     * @param version1
     * @param version2
     * @return 若version1>version2 返回1，version1==version2 返回0，version1<version2 返回-1
     */
    public static int compareVersion(String version1, String version2){
        if (version1.equals(version2)) {
            return 0;
        }
        String[] v1s = version1.split("\\.");
        String[] v2s = version2.split("\\.");
        int maxlen = Math.max(v1s.length, v2s.length);
        Integer v1 = null;
        Integer v2 = null;
        for (int i = 0; i<maxlen; i++) {
            if (i < v1s.length) {
                v1 = Integer.valueOf(v1s[i]);
            } else {
                v1 = 0;
            }
            if (i < v2s.length) {
                v2 = Integer.valueOf(v2s[i]);
            } else {
                v2 = 0;
            }

            if (v1.equals(v2)) {
                continue;
            } else {
                return v1.compareTo(v2);
            }
        }
        return 0;
    }

    public static void main(String[] args) {
        System.out.println(compareVersion("0.9.9.9.9.9.9", "1.0"));
        System.out.println(compareVersion("1.00000000000000000000001", "1.10"));
        System.out.println(compareVersion("01", "1"));
        System.out.println(compareVersion("1", "1.1"));
        System.out.println(compareVersion("2.100000000.0", "2.1"));
        System.out.println(compareVersion("1.0.3", "1.0.3.1"));
        System.out.println(compareVersion("1.0.3", "1.0.4"));
    }
}
